package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.domain.TravelContent;
import cn.wolfcode.luowowo.article.mapper.TravelContentMapper;
import cn.wolfcode.luowowo.article.mapper.TravelMapper;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.ITravelService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class TravelServiceImpl implements ITravelService {
    @Autowired
    private TravelMapper travelMapper;

    @Autowired
    private TravelContentMapper travelContentMapper;

    @Override
    public PageInfo query(TravelQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<Travel> listData = travelMapper.selectForList(qo);
        return new PageInfo(listData);
    }

    @Override
    public Travel get(Long id) {
        Travel travel = travelMapper.selectByPrimaryKey(id);
        travel.setTravelContent(travelContentMapper.selectByPrimaryKey(id));
        return travel;
    }

    @Override
    public TravelContent getContent(Long id) {
        return travelContentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Travel> getViewNumTop3ByDestId(Long destId, Long travelId) {
        return travelMapper.selectViewNumTop3ByDestId(destId, travelId);
    }

    @Override
    public Long saveOrUpdate(Travel travel) {
        TravelContent travelContent = travel.getTravelContent();
        String content = travelContent.getContent();
        if(content.length() > 100){
            travel.setSummary(content.substring(0,100));
        }else {
            travel.setSummary(content);
        }
        if (travel.getId() == null) {
            travel.setCreateTime(new Date());
            travelMapper.insert(travel);
            travelContent.setId(travel.getId());
            travelContentMapper.insert(travelContent);
        } else {
            travel.setLastUpdateTime(new Date());
            travelMapper.updateByPrimaryKey(travel);
            travelContent.setId(travel.getId());
            travelContentMapper.updateByPrimaryKey(travelContent);
        }
        return travel.getId();
    }

    @Override
    public List<Travel> list() {
        return travelMapper.selectAll();
    }

    @Override
    public void updateState(Long id, Integer state) {
        travelMapper.updateState(id, state);
    }

    @Override
    public List<Travel> getTravelTop3ByUserId(Long id) {
        List<Travel> travels = travelMapper.selectTravelTop3ByUserId(id);
        return travels;
    }

    @Override
    public List<Travel> getTravelsByUserId(Long userId) {
        return travelMapper.selectTravelsByUserId(userId);
    }

    @Override
    public void saveFavorToUser(Long sid, Long userId) {
        travelMapper.insertFavorRelation(sid, userId);
    }

    @Override
    public void deleteUserFavor(Long userId, Long sid) {
        travelMapper.deleteUserFavor(userId, sid);
    }
}
